import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiAlarmApi } from '#/api/moxi/aialarm';
import Dayjs from 'dayjs';
import { ref } from 'vue';
import { getRangePickerDefaultProps, } from '#/utils';

import { getSelectList } from '#/api/moxi/aialarm';
// 获取报警类型
const deptOptions = ref<Array<{ label: string; value: string }>>([]);
const loadDeptOptions = async () => {
    try {
        const response = await getSelectList();
        console.log('类型');
        console.log(response);
        deptOptions.value = response.map(r => r.typeName && r.typeCode ? ({
            label: r.typeName,
            value: r.typeCode
        }) : []);
    } catch (error) {
        console.error('获取报警类型失败:', error);
    }
};
loadDeptOptions();

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
    return [
        {
            fieldName: 'objName',
            label: '告警对象名称',
            component: 'Input',
            componentProps: {
                placeholder: '请输入抓拍摄像头名称',
                allowClear: true,
            },
        },
        {
            fieldName: 'typeCode',
            label: '报警类型',
            component: 'Select',
            componentProps: {
                options: deptOptions,
                placeholder: '请选择部门',
                allowClear: true,
            },
        },
        {
            fieldName: 'alarmTime',
            label: '报警时间',
            component: 'RangePicker',
            componentProps: {
                ...getRangePickerDefaultProps(),
                allowClear: true,
            },
        },
    ];
}

/** 列表的字段 */
export function useGridColumns<T = AiAlarmApi.AiAlarm>(
    onStatusChange?: (
        newStatus: number,
        row: T,
    ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
    return [
        {
            field: 'id',
            title: '序号',
        },
        {
            field: 'objName',
            title: '告警对象名称',
        },
        {
            field: 'typeName',
            title: '报警类型',
        },
        {
            field: 'ofDate',
            title: '告警时间',
        },
        {
            field: 'falseAlarmTime',
            title: '误报确认时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '';
            },
        },
        {
            field: 'falseAlarmDesc',
            title: '误报描述',
        },
        {
            title: '操作',
            width: 180,
            fixed: 'right',
            slots: { default: 'actions' },
        },
    ];
}
